package com.example.controller;



import com.example.bean.*;
import com.example.service.LoginService;
import com.example.utils.ApiResultHandler;
import com.example.utils.Md5;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/login")
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;

    @GetMapping("/judge/{userPhone}/{userIdentity}/{card}")
    public ApiResult judgeLogin(@PathVariable("userPhone") String userPhone, @PathVariable("userIdentity") String userIdentity, @PathVariable("card") String card){
        User res = loginService.judgeLogin(userPhone, userIdentity, card);
        if(res==null){
            return ApiResultHandler.buildApiResult(200,"查无此人",res);

        }else return ApiResultHandler.buildApiResult(400,"已经注册",res);
    }

    @PostMapping("/phone")
    public ApiResult userPhoneLogin(@RequestBody Login login){
        String loginId = login.getLoginId();
        String loginPassword = login.getPassword();
        String password = Md5.md5(loginPassword);
        User res = loginService.userPhoneLogin(loginId, password);
        if (res!=null) return ApiResultHandler.buildApiResult(200,"登录成功",res);
        return ApiResultHandler.buildApiResult(400,"登录信息无效，请重新登录",res);
    }
    @PostMapping("/identity")
    public ApiResult userIdentityLogin(@RequestBody Login login){
        String loginId = login.getLoginId();
        String loginPassword = login.getPassword();
        String password = Md5.md5(loginPassword);
        User res = loginService.userIdentityLogin(loginId, password);
        if (res!=null) return ApiResultHandler.buildApiResult(200,"登录成功",res);
        return ApiResultHandler.buildApiResult(400,"登录信息无效，请重新登录",res);
    }
    @PostMapping("/card")
    public ApiResult cardLogin(@RequestBody Login login){
        String loginId = login.getLoginId();
        String loginPassword = login.getPassword();
        String password = Md5.md5(loginPassword);
        User res = loginService.cardLogin(loginId, password);
        if (res!=null) return ApiResultHandler.buildApiResult(200,"登录成功",res);
        return ApiResultHandler.buildApiResult(400,"登录信息无效，请重新登录",res);
    }
    @PostMapping("/admin")
    public ApiResult adminLogin(@RequestBody Login login){
        String loginId = login.getLoginId();
        String loginPassword = login.getPassword();
        String password = Md5.md5(loginPassword);
        Admin res = loginService.adminLogin(loginId, password);
        if (res!=null) return ApiResultHandler.buildApiResult(200,"登录成功",res);
        return ApiResultHandler.buildApiResult(400,"登录信息无效，请重新登录",res);
    }
    @PostMapping("/superAdmin")
    public ApiResult superAdmLogin(@RequestBody Login login){
        String loginId = login.getLoginId();
        String loginPassword = login.getPassword();
        String password = Md5.md5(loginPassword);
        SuperAdmin res = loginService.superAdmLogin(loginId, password);
        if (res!=null) return ApiResultHandler.buildApiResult(200,"登录成功",res);
        return ApiResultHandler.buildApiResult(400,"登录信息无效，请重新登录",res);
    }

}
